package cn.yeyiu.sexyiu.task;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AlertDialog;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.util.NetConfig;
import cn.yeyiu.util.NetUtil;

import com.google.android.material.textfield.TextInputEditText;

import okhttp3.HttpUrl;

public class InitIPTask extends AsyncTask<Void, Void, String> {
    private final Activity activity;
    private AlertDialog dialog;
    private final AlertDialog.Builder mDialogBuilder;

    private void startMain() {
    }

    public InitIPTask(Activity activity2) {
        this.activity = activity2;
        this.mDialogBuilder = new AlertDialog.Builder(activity2);
    }

    /* access modifiers changed from: protected */
    public void onPreExecute() {
        AlertDialog create = this.mDialogBuilder.setView(R.layout.dialog_finding_server).setCancelable(false).create();
        this.dialog = create;
        create.getWindow().setBackgroundDrawable(new ColorDrawable(this.activity.getColor(R.color.transport)));
        this.dialog.show();
    }

    /* access modifiers changed from: protected */
    public String doInBackground(Void... voidArr) {
        String quickServerIP = NetUtil.getQuickServerIP();
        this.dialog.dismiss();
        return quickServerIP;
    }

    /* access modifiers changed from: protected */
    public void onPostExecute(String str) {
        if (!NetConfig.setIp(this.activity, str)) {
            inputIP();
        } else {
            startMain();
        }
    }

    private void inputIP() {
        View inflate = LayoutInflater.from(this.activity).inflate(R.layout.dialog_input_host, (ViewGroup) null);
        this.dialog = this.mDialogBuilder
                .setView(inflate)
                .setCancelable(false)
                .setTitle("请输入HOST IP地址")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String ip = ((TextInputEditText) inflate.findViewById(R.id.et_host_ip)).getText().toString();
                        if ("".equals(ip)) {
                            activity.finish();
                            return;
                        }
                        NetConfig.setIp(activity, ip);
                        startMain();
                    }
                })
                .create();
        this.dialog.show();
    }

    public /* synthetic */ void lambda$inputIP$0$InitIPTask(View view, DialogInterface dialogInterface, int i) {
        String obj = ((TextInputEditText) view.findViewById(R.id.et_host_ip)).getText().toString();
        if (HttpUrl.FRAGMENT_ENCODE_SET.equals(obj)) {
            this.activity.finish();
            return;
        }
        NetConfig.setIp(this.activity, obj);
        startMain();
    }
}
